package com.buddha.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buddha.api.common.Result;
import com.buddha.api.entity.CarPicture;
import com.buddha.api.service.impl.CarPictureServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2023-07-06
 */
@CrossOrigin(origins = {"*"})
@RestController
@RequestMapping("/pic")
public class CarPictureController {
    @Resource
    CarPictureServiceImpl pictureService;

    @PostMapping("/save")
    public Result AddCar(@RequestBody CarPicture picture){
        boolean flag;
        if(picture.getPicId() == null){
            flag = pictureService.save(picture);
            return flag?Result.success():Result.error("300","添加失败");
        }else{
            pictureService.updateById(picture);
            return Result.success();
        }
    }

    @GetMapping("/get")
    public Result GetCar(@RequestParam Integer carId){
        QueryWrapper<CarPicture> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id",carId);
        List<CarPicture> pictureList= pictureService.list(wrapper);
        return Result.success(pictureList);
    }

    @PostMapping("/del")
    public Result DelCar(@RequestBody Integer picId){
        boolean flag = pictureService.removeById(picId);
        return flag?Result.success():Result.error();
    }

    @GetMapping("/id")
    public Result getCarById(@RequestParam Integer carId){
        QueryWrapper<CarPicture> carPictureQueryWrapper = new QueryWrapper<>();
        carPictureQueryWrapper.eq("car_id",carId);
        List<CarPicture> carPictureList = pictureService.list(carPictureQueryWrapper);
        return Result.success(carPictureList);
    }
}
